package com.ewzj.bigdata.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.di.component.DaggerCollectComponent;
import com.ewzj.bigdata.di.module.CollectModule;
import com.ewzj.bigdata.mvp.contract.CollectContract;
import com.ewzj.bigdata.mvp.model.entity.resbean.CollectBean;
import com.ewzj.bigdata.mvp.presenter.CollectPresenter;
import com.ewzj.bigdata.mvp.ui.activity.CollectUpdateAndCreateActivity;
import com.ewzj.bigdata.mvp.ui.adapter.CollectAdapter;
import com.ewzj.bigdata.mvp.ui.widget.InputDialog;
import com.jess.arms.base.App;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.mylhyl.circledialog.CircleDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class CollectFragment extends BaseFragment<CollectPresenter> implements CollectContract.View {


    @BindView(R.id.rv_collect)
    RecyclerView rvCollect;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    private List<CollectBean> collectBeans;
    private CollectAdapter collectAdapter;
    public static CollectFragment newInstance() {
        CollectFragment fragment = new CollectFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerCollectComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .collectModule(new CollectModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_collect, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initAdapter();
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.swiperefresh();
            }
        });

//        mPresenter.loadinitdata();
    }

    private void initAdapter() {
        if (collectBeans==null){
            collectBeans=new ArrayList<>();
        }
        if (collectAdapter==null){
            collectAdapter=new CollectAdapter(collectBeans);
        }
        rvCollect.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvCollect.setAdapter(collectAdapter);
        collectAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.iv_edit:
                        mPresenter.editCollectBean(((CollectBean)(adapter.getData().get(position))));
                        Intent intent=new Intent(getActivity(), CollectUpdateAndCreateActivity.class);
                        intent.putExtra(AppConstants.EXTRA_DATA,((CollectBean)(adapter.getData().get(position))));
                        intent.putExtra(AppConstants.EXTRA_DATA_STRING,((CollectBean)(adapter.getData().get(position))).getTitle());
                        ArmsUtils.startActivity(intent);
                        break;
                    case R.id.iv_del:
                        showDelDialog(adapter,position);
                        break;
                }
            }
        });
        /**
         *
         * 暂时不需要加载更多功能
         */
        collectAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadmoreData();
            }
        },rvCollect);
        collectAdapter.disableLoadMoreIfNotFullPage();
    }

    private void showDelDialog(BaseQuickAdapter adapter, int position) {
        new CircleDialog.Builder(getActivity())
                .setCanceledOnTouchOutside(false)
                .setCancelable(false)
                .setTitle(((CollectBean)(adapter.getData().get(position))).getTitle())
                .setText("确定要删除"+((CollectBean)(adapter.getData().get(position))).getTitle())
                .setNegative("取消", null)
                .setPositive("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        adapter.getData().remove(((CollectBean)adapter.getData().get(position)));
                        adapter.notifyDataSetChanged();
                        mPresenter.delCollectBean(((CollectBean)adapter.getData().get(position)));
                    }
                })
                .show();
    }

    /**
     * 此方法是让外部调用使fragment做一些操作的,比如说外部的activity想让fragment对象执行一些方法,
     * 建议在有多个需要让外界调用的方法时,统一传Message,通过what字段,来区分不同的方法,在setData
     * 方法中就可以switch做不同的操作,这样就可以用统一的入口方法做不同的事
     * <p>
     * 使用此方法时请注意调用时fragment的生命周期,如果调用此setData方法时onCreate还没执行
     * setData里却调用了presenter的方法时,是会报空的,因为dagger注入是在onCreated方法中执行的,然后才创建的presenter
     * 如果要做一些初始化操作,可以不必让外部调setData,在initData中初始化就可以了
     *
     * @param data
     */

    @Override
    public void setData(Object data) {

    }


    @Override
    public void showLoading() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @OnClick(R.id.iv_add)
    public void onViewClicked() {
//        showAddDialog();
        Intent intent=new Intent(getActivity(),CollectUpdateAndCreateActivity.class);
        intent.putExtra(AppConstants.EXTRA_DATA_STRING,"创建分组");
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void adapterSwipeRefreh(List<CollectBean> collectBeans) {
        collectAdapter.setNewData(collectBeans);
    }

    @Override
    public void adapterInsert(List<CollectBean> collectBeans) {
        collectAdapter.addData(collectBeans);
    }

    @Override
    public void adapterInsert(CollectBean collectBeans) {
        collectAdapter.addData(collectBeans);
    }

    @Override
    public void adapterRemove(CollectBean collectBean) {
        for (int i = 0; i <collectAdapter.getData().size() ; i++) {
            if (collectBean==collectAdapter.getData().get(i)){
                collectAdapter.remove(i);
                break;
            }
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        if (collectBeans!=null){
            collectBeans.clear();
            mPresenter.loadinitdata();
        }
    }

    @Override
    public CollectAdapter obtainAdapter() {
        if (collectAdapter==null){
            collectAdapter=new CollectAdapter(collectBeans);
        }
        return collectAdapter;
    }
    private void showAddDialog(){
        InputDialog inputDialog=InputDialog.getInstance();
        inputDialog.setAddlistener(new InputDialog.Addlistener() {
            @Override
            public void add(String title) {
                mPresenter.addCollectBean(title);
            }
        });
        inputDialog.show(getChildFragmentManager(),"CreateCollect");
    }
}
